package service

import (
	"beesoft.ink/application/garage/app/g"
	"beesoft.ink/application/garage/app/model"
	"errors"
	"time"
)

func (r auth) LoginByUsernamePassword(req *model.AuthLoginReq) (*model.User, error) {
	var item *model.User
	if err := g.DB().Where("user", req.Username).First(&item).Error; err != nil {
		return nil, err
	}

	if item != nil && item.Password != req.Password {
		return nil, errors.New("username or password error")
	}

	return item, nil
}
func (r auth) LoginByEmailVerificationCode(req *model.AuthLoginReq) (*model.User, error) {
	var item *model.User
	if err := g.DB().Where("email", req.Email).First(&item).Error; err != nil {
		return nil, err
	}

	if item == nil || item.VerificationCode != req.VerificationCode || item.VerificationCodeExpiredAt.Sub(time.Now()).Seconds() > 60 {
		return nil, errors.New("login error")
	}

	return item, nil
}
func (r auth) LoginByPhoneVerificationCode(req *model.AuthLoginReq) (*model.User, error) {
	var item *model.User
	if err := g.DB().Where("phone", req.Phone).First(&item).Error; err != nil {
		return nil, err
	}

	if item == nil || item.VerificationCode != req.VerificationCode || item.VerificationCodeExpiredAt.Sub(time.Now()).Seconds() > 60 {
		return nil, errors.New("login error")
	}

	return item, nil
}
